/**
 * Creates a map of exported modules, allowing the RNW babel plugin to rewrite
 * paths only for modules it knows are exported by RNW.
 */
const fs = require('fs');
const path = require('path');

const isDirectory = (source) => fs.lstatSync(source).isDirectory();
const getDirectories = (source) =>
  fs.readdirSync(source).filter((name) => isDirectory(path.join(source, name)));

const packagesDir = path.join(__dirname, '../../packages/');
const exportsDir = path.join(packagesDir, 'react-native-web/src/exports');
const moduleMapOutfile = path.join(packagesDir, 'babel-plugin-react-native-web/src/moduleMap.js');

const moduleMap = getDirectories(exportsDir).reduce((acc, curr) => {
  acc[curr] = true;
  return acc;
}, {});

const data = `// THIS FILE IS AUTOMATICALLY GENERATED. DO NOT EDIT.
module.exports = ${JSON.stringify(moduleMap, null, 2)}`;

fs.writeFileSync(moduleMapOutfile, data);
